EmailPasswdEditPortlet = Class.extend({
	init: function()	{
		this.name = "EmailPasswdEditPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onWhoIsSelectChanged: function()	{
		var whois = this.requestForEffectiveResource('WhoIs').find('option:selected').val();
		if (whois == 1)	{
			this.requestForEffectiveResource('OptionalArea').show();
			this.requestForEffectiveResource('CompoundStudentID').show();
		} else {
			this.requestForEffectiveResource('OptionalArea').hide();
			this.requestForEffectiveResource('CompoundStudentID').hide();
		}
	},
	
	onUserCPButtonClick: function()	{
		var curPass = this.requestForEffectiveResource('CurrentPasswd').val().trim();
		var pass = this.requestForEffectiveResource('Passwd').val().trim();
		var confirmPass = this.requestForEffectiveResource('ConfirmPasswd').val().trim();
		var email = this.requestForEffectiveResource('Email').val().trim();
		
		var whois = this.requestForEffectiveResource('WhoIs').find('option:selected').val();
		var studentID = this.requestForEffectiveResource('StudentID').val();
		if (studentID.length != 8 && studentID.length != 0)	{
			this.requestForEffectiveResource('StudentIDError').html('Bạn nhập không đúng mã số sinh viên.');
			return;
		}
		
		this.requestForEffectiveResource('Error').html('');
		var obj = this;
		this.onAjax('user-ajax', 'edit-email-password', {'currentPasswd': curPass, 'passwd': pass, 'confirmPassword': confirmPass, 'email':email, 'whois':whois, 'sid':studentID}, 'POST', {
			'onSuccess': function(ret)	{
				var subject = SingletonFactory.getInstance(Subject);
				subject.notifyEvent('NotifyGlobal', 'Bạn đã thay đổi thông tin cá nhân thành công!');
				subject.notifyEvent('RequestRoute', new Request('User'));
			},
			'onFailure': function(message)	{
				var err = {};
				err.errors = message;
				obj.requestForEffectiveResource('Error').html(obj.renderView('ErrorView', err));
			}
		});
	},
	
	run: function()	{
		var obj = this;
		this.getPortletPlaceholder().paintCanvas(this.render());
		this.onAjax('user-ajax', 'get-user-email', {}, 'GET', {
			'onSuccess': function(ret)	{
				obj.requestForEffectiveResource('Email').val(ret);
			}
		});
		this.onAjax('user-ajax', 'get-whois', {}, 'GET', {
			'onSuccess': function(ret)	{
				obj.requestForEffectiveResource('StudentID').val(ret.sid);
				obj.requestForEffectiveResource('WhoIs').val(ret.whois);
				var subject = SingletonFactory.getInstance(Subject);
				subject.notifyEvent('WhoIsSelectChanged');
			}
		});
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);

EmailRecoverPortlet = Class.extend({
	init: function()	{
		this.name = "EmailRecoverPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onRecoverButtonClick: function()	{
		var obj = this;
		var captchaResponse = $('input#recaptcha_response_field').val();
		var captchaChallenge = $('input#recaptcha_challenge_field').val();
		var email = this.requestForEffectiveResource('Email').val();
		this.requestForEffectiveResource('Email').attr('disabled', 'true');
		this.requestForEffectiveResource('Error').html('<img src="static/images/ajax-loader-1.gif">');
		
		this.onAjax('user-ajax', 'request-new-password', 
				{'recaptcha_challenge_field': captchaChallenge, 'recaptcha_response_field': captchaResponse,
					'email': email}, 'POST', {
						'onSuccess': function()	{
							var subject = SingletonFactory.getInstance(Subject);
							subject.notifyEvent('NotifyGlobal', 'Chúng tôi đã gửi hướng dẫn phục hồi mật khẩu đến email của bạn! Hướng dẫn có giá trị trong vòng 24h');
							subject.notifyEvent('RequestRoute', new Request('Home'));
						},
						
						'onFailure': function(message)	{
							obj.requestForEffectiveResource('Email').removeAttr('disabled');
							obj.requestForEffectiveResource('Error').html(message);
						}
					});
	},
		
	run: function()	{
		this.getPortletPlaceholder().paintCanvas(this.render());
		Recaptcha.create("6LeF-MISAAAAAKXfmjbbhb-EO5wSfJK3qkRBfGD_",
			    this.name+"-Recaptcha",
			    {
			      theme: "red",
			      callback: Recaptcha.focus_response_field
			    }
			  );
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);

PasswordResetPortlet = Class.extend({
	init: function()	{
		this.name = "PasswordResetPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onReloadPage: function()	{
		this.run();
	},
	
	onResetButtonClick: function()	{
		var obj = this;
		var pw = this.requestForEffectiveResource('Password').val();
		var cfpw = this.requestForEffectiveResource('ConfirmPassword').val();
		if (pw != cfpw)	{
			obj.requestForEffectiveResource('Error').html('Hai mật khẩu không giống nhau');
			return;
		}
		
		var token = this.token;
		if (token == undefined)	{
			token = this.getRequest().getParam('token');
		}
		
		this.onAjax('user-ajax', 'reset-password', 
				{'token': token, 'passwd': pw}, 'POST', {
						'onSuccess': function()	{
							var subject = SingletonFactory.getInstance(Subject);
							subject.notifyEvent('NotifyGlobal', 'Bạn đã thay đổi mật khẩu thành công');
							subject.notifyEvent('RequestRoute', new Request('Login'));
						},
						
						'onFailure': function(message)	{
							obj.requestForEffectiveResource('Error').html(message);	
						}
					});
	},
		
	run: function()	{
		this.getPortletPlaceholder().paintCanvas('');
		var token = this.getRequest().getParam('token', undefined);
		if (token == undefined)	{
			this.getPortletPlaceholder().paintCanvas(this.renderView('ErrorInvalidToken', {}));
		} else {
			var obj = this;
			this.onAjax('user-ajax', 'is-token-valid', {'token':token}, 'GET', {
				'onSuccess': function(ret)	{
					obj.token = token;
					obj.getPortletPlaceholder().paintCanvas(obj.render());
				},
				'onFailure': function(message)	{
					obj.getPortletPlaceholder().paintCanvas(obj.renderView('ErrorInvalidToken', {}));
				}
			});
		}
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);